package jason.service.impl;

import io.seata.spring.annotation.GlobalTransactional;
import jason.dao.OrderDao;
import jason.domain.Order;
import jason.service.AccountService;
import jason.service.OrderService;
import jason.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    /**
     * 创建订单->调用库存服务扣减库存->调用账户服务扣减账户余额->修改订单状态
     * 简单说：
     * 下订单->减库存->减余额->改状态
     */
    @Override
    @GlobalTransactional(name = "order",rollbackFor = Exception.class)
    public void create(Order order) {

        orderDao.create(order);

        log.info("订单微服务开始调用库存,做扣减");

        storageService.decrease(order.getProductId(),order.getCount());

        log.info("订单完成扣减");
        log.info("账户准备开始做扣减");

        accountService.decrease(order.getUserId(),order.getMoney());

        log.info("调用账户微服务，扣减完毕");
        //修改订单状态
        log.info("修改订单状态开始");

        orderDao.update(order.getUserId(),0);

        log.info("修改订单状态结束");
        return ;
    }
}
